package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import mainController.ClientController;

import view.ClientLoginViewPanel;

public class ClientLoginController {
	
	private ClientLoginViewPanel panel;
	private ClientController clientController;
	
	public ClientLoginController(ClientController clientController) {
		this.clientController = clientController;
		this.panel = new ClientLoginViewPanel();
		
		this.panel.getjButtonConfirm().addActionListener(new ListenerConfirm());
	}
	
	public JPanel getPanel() {
		return this.panel;
	}
	
	class ListenerConfirm implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String code = ClientLoginController.this.panel.getjTextFieldCode().getText();
			try {
				ClientLoginController.this.clientController.handshake(code);
			}
			catch (UnknownHostException e1) {
				JOptionPane.showMessageDialog(ClientLoginController.this.panel,
					    "Conexão não pode ser estabelecida.\nServidor desconhecido.\n " , 
					    "Acesso negado", JOptionPane.ERROR_MESSAGE);
				//System.err.println("ListenerConfirm:: " + e1);
	            //System.exit(1);
			}
			catch (IOException e2) {
				JOptionPane.showMessageDialog(ClientLoginController.this.panel,
					    "Conexão não pode ser estabelecida.\nVerifique o código do sensor.\n " , 
					    "Acesso negado", JOptionPane.ERROR_MESSAGE);			
	            //System.err.println("ListenerConfirm:: " + e2);
	            //System.exit(1);
	        }			
			catch (ClassNotFoundException e3) {
				System.err.println("ListenerConfirm:: " + e3);
	            //System.exit(1);				
			}	
		}
	}	
}
